package com.ihg.hiex.admin.dao.session;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.Status;
import com.ihg.hiex.entity.session.participator.SessionParticipator;

/**
 *
 * @author Ark
 *
 */
@Dao("hibernate")
public interface SessionParticipatorDao {

    /**
     * @param session
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session")
    public List<SessionParticipator> list(@Variable("session") Session session);
    
    /**
     * @param session
     * @param subSession
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND subSession = :subSession ORDER BY subGroup, assessorSeat, delegateSeat")
    public List<SessionParticipator> list(@Variable("session") Session session, @Variable("subSession") String subSession);
    
    /**
     * list assessor of session
     * 
     * @param session
     * @return
     */
    @Query("SELECT DISTINCT assessorId FROM SessionParticipator WHERE disabled = false AND session = :session")
    public List<Long> listAssessor(@Variable("session") Session session);
    
    /**
     * list delegate of session
     * 
     * @param session
     * @return
     */
    @Query("SELECT delegateId FROM SessionParticipator WHERE disabled = false AND session = :session")
    public List<Long> listDelegate(@Variable("session") Session session);
    
    /**
     * list delegate of sessions
     * 
     * @param sessions
     * @return
     */
    @Query("SELECT DISTINCT delegateId FROM SessionParticipator WHERE disabled = false AND session IN (:sessions)")
    public List<Long> listDelegate(@Variable("sessions") List<Session> sessions);
    
    /**
     * @param status
     * @param assessorId
     * @return
     */
    @Query("SELECT DISTINCT(participator.session) FROM SessionParticipator participator WHERE participator.disabled = false AND participator.session.disabled = false AND participator.session.sessionStatus = :status AND participator.assessorId = :assessorId")
    public List<Session> listSessionByAssessor(@Variable("status") Status status, @Variable("assessorId") Long assessorId);
    
    /**
     * @param status
     * @param delegateId
     * @return
     */
    @Query("SELECT participator.session FROM SessionParticipator participator WHERE participator.disabled = false AND participator.session.disabled = false AND participator.session.sessionStatus = :status AND participator.delegateId = :delegateId")
    public List<Session> listBySessionDelegate(@Variable("status") Status status, @Variable("delegateId") Long delegateId);
    
    /**
     * @param session
     * @param subSession
     * @param subgroup
     * @param delegateSeat
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND subSession = :subSession AND subgroup = :subgroup AND delegateSeat = :delegateSeat")
    public SessionParticipator load(@Variable("session") Session session, @Variable("subSession") String subSession, @Variable("subgroup") String subgroup, @Variable("delegateSeat") String delegateSeat);
    
    /**
     * 
     * @param session
     * @param assessorId
     */
    @Execute("UPDATE SessionParticipator SET assessorId = NULL, assessorName = NULL WHERE disabled = false AND session = :session AND assessorId = :assessorId")
    public void assessorRemove(@Variable("session") Session session, @Variable("assessorId") Long assessorId);
    
    /**
     * 
     * @param session
     * @param delegateId
     */
    @Execute("UPDATE SessionParticipator SET delegateId = NULL, delegateName = NULL WHERE disabled = false AND session = :session AND delegateId = :delegateId")
    public void delegateRemove(@Variable("session") Session session, @Variable("delegateId") Long delegateId);
    
    /**
     * 
     * @param assessorId
     * @param assessorName
     * @param session
     * @param subSession
     * @param subgroup
     * @param delegateSeats
     */
    @Execute("UPDATE SessionParticipator SET assessorId = :assessorId, assessorName = :assessorName WHERE disabled = false AND session = :session AND subSession = :subSession AND subgroup = :subgroup AND delegateSeat in (:delegateSeats)")
    public void addAssessor(@Variable("assessorId") Long assessorId, @Variable("assessorName") String assessorName, @Variable("session") Session session, @Variable("subSession") String subSession, @Variable("subgroup") String subgroup, @Variable("delegateSeats") String[] delegateSeats);
    
    /**
     * 
     * @param subSession
     * @param session
     * @param subGroups
     */
    @Execute("UPDATE SessionParticipator SET subSession = :subSession WHERE disabled = false AND session = :session AND subgroup IN (:subGroups)")
    public void changeSubSession(@Variable("subSession")String subSession, @Variable("session") Session session, @Variable("subGroups") String[] subGroups);
    
    /**
     * delete participartor by session
     * 
     * @param session
     */
    @Execute("UPDATE SessionParticipator SET disabled = true WHERE session = :session")
    public void delete(@Variable("session") Session session);
    
    /**
     * 
     * @param session
     * @param assessorId
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND assessorId = :assessorId")
    public List<SessionParticipator> list(@Variable("session") Session session, @Variable("assessorId") Long assessorId);
    
    /**
     * 
     * @param session
     * @param subgroup
     * @param assessorId
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND subgroup = :subgroup AND assessorId != :assessorId")
    public List<SessionParticipator> list(@Variable("session") Session session, @Variable("subgroup") String subgroup, @Variable("assessorId") Long assessorId);

    /**
     * 
     * @param session
     * @param subgroup
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND subgroup = :subgroup")
    public List<SessionParticipator> listByGroup(@Variable("session") Session session, @Variable("subgroup") String subgroup);

    /**
     * 
     * @param session
     * @param delegateId
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND delegateId = :delegateId")
    public SessionParticipator loadByDelegate(@Variable("session") Session session, @Variable("delegateId") Long delegateId);

    /**
     * 
     * @param delegateId
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND delegateId = :delegateId")
    public SessionParticipator loadByDelegate(@Variable("delegateId") Long delegateId);

    /**
     * 
     * @param session
     * @param assessorId
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND assessorId = :assessorId")
    public SessionParticipator loadByAssessor(@Variable("session") Session session, @Variable("assessorId") Long assessorId);

    /**
     * 
     * @param session
     * @param subgroup
     * @param assessorId
     * @return
     */
    @Query("FROM SessionParticipator WHERE disabled = false AND session = :session AND subgroup = :subgroup AND assessorId != :assessorId")
    public SessionParticipator load(@Variable("session") Session session, @Variable("subgroup") String subgroup, @Variable("assessorId") Long assessorId);
    
}
